package com.aivin.tvslidebar.scrool2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;

import java.util.List;


/**
 * 字符串滚动选择器
 */
public class StringScrollPicker extends ScrollPickerView {
    private final TextPaint mPaint;

    /**
     * 最小的字体
     */
    private int mMinTextSize = 28;
    /**
     * 最大的字体
     */
    private int mMaxTextSize = 38;
    /**
     * 最大的行宽,默认为itemWidth.超过后文字自动换行
     */
    private int mMaxLineWidth = 300;
    /**
     * 对齐方式,默认居中
     */
    private Layout.Alignment mAlignment = Layout.Alignment.ALIGN_CENTER;


    public StringScrollPicker(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StringScrollPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.BLACK);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mMaxLineWidth < 0) {
            mMaxLineWidth = getItemWidth();
        }
    }

    @Override
    public void drawItem(Canvas canvas, List<String> data, int position, int relative, float moveLength, float leftX) {
        CharSequence text = data.get(position);
        int itemSize = getItemSize();

        // 设置文字大小
        if (relative == -1) {
            // 左边的item
            if (moveLength < 0) {
                // 向右滚动的距离
                mPaint.setTextSize(mMinTextSize);
            } else { // 向下滑动
                mPaint.setTextSize(mMinTextSize + (mMaxTextSize - mMinTextSize) * moveLength / itemSize);
            }
        } else if (relative == 0) {
            // 中间item,当前选中
            mPaint.setTextSize(mMinTextSize + (mMaxTextSize - mMinTextSize) * (itemSize - Math.abs(moveLength)) / itemSize);
        } else if (relative == 1) {
            // 右边的item
            if (moveLength > 0) {
                // 向下滑动
                mPaint.setTextSize(mMinTextSize);
            } else {
                mPaint.setTextSize(mMinTextSize + (mMaxTextSize - mMinTextSize) * -moveLength / itemSize);
            }
        } else {
            // 其他
            mPaint.setTextSize(mMinTextSize);
        }

        // 设置文字颜色颜色
        computeColor(relative);

        // 使用 StaticLayout 支持换行
        StaticLayout layout = new StaticLayout(text, 0, text.length(), mPaint, mMaxLineWidth, mAlignment, 1.0F, 0.0F, true, null, 0);
        float lineWidth = layout.getWidth();
        float x = leftX + (getItemWidth() - lineWidth) / 2;
        float y = (getItemHeight() * 1f - layout.getHeight()) / 2;

        canvas.save();
        canvas.translate(x, y);
        layout.draw(canvas);
        canvas.restore();
    }

    /**
     * 计算字体颜色，渐变
     *
     * @param relative 　相对中间item的位置
     */
    private void computeColor(int relative) {
        if (relative == 0) {
            mPaint.setColor(Color.RED);
        } else {
            mPaint.setColor(Color.GRAY);
        }
    }

}
